package com.mdx.tools.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.parser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.*;
import java.util.function.Supplier;

public class JsonUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String objectToJson(Object obj){
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    //字符串转map
    public static Map<String,Object> stringToMap(String str){
        try {
            return objectMapper.readValue(str,Map.class);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    //字符串转map
    public static Map<String,Object> jsonToMap(String str){
        try {
            Map map = JSON.parseObject(str, Map.class , Feature.OrderedField);
            map.forEach((k,v)->{
                if(v instanceof JSONArray){
                    JSONArray array=(JSONArray)v;
                    map.put(k,String.join(",", array.toJavaList(String.class)));
                }
            });
            return map;
        } catch (Exception e) {
            return null;
        }
    }

    //字符串转map
    public static Map stringToMap(Object obj){
        try {
            return objectMapper.readValue(obj.toString(),Map.class);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    //字符串转map
    public static Map<String,Object> stringToMap(Supplier<String> str){
        try {
            return objectMapper.readValue(str.get(),Map.class);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    //map转字符串
    public static String mapTostring(Map map){
        try {
            return objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    // 将Bean转成Map
    public static Map<String,Object> beanToMap(Object obj){
        try {
            return objectMapper.readValue(objectToJson(obj), Map.class);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    // 将字符串转换成list ，号分割
    public static List<String> stringsToList(String str){
        String[] strings = str.split(",");
        return Arrays.asList(strings);
    }

    /**
     * 首字母小写
     * author：wp
     */
    public static Object keyFirstToLower(Object obj) throws Exception {
        if(obj != null){
            if(obj instanceof List){
                List<Map<String,Object>> list = (List<Map<String, Object>>) obj;
                List<Map<String,Object>> listRes = new ArrayList<Map<String,Object>>();
                if(list != null && list.size() > 0){
                    for (Map<String, Object> map : list) {
                        if (map.containsKey("SPBillID")){
                            map.put("spbillID",map.get("SPBillID"));
                            map.remove("SPBillID");

                        }
                        if (map.containsKey("SPBillType")){
                            map.put("spbillType",map.get("SPBillType"));
                            map.remove("SPBillType");
                        }
                        listRes.add(toLower(map));
                    }
                    return listRes;
                }
            }else if(obj instanceof Map){
                Map<String,Object> result = new HashMap<String,Object>();
                Map<String,Object> map = (Map<String, Object>) obj;
                if(map!= null && !map.isEmpty()){
                    result = toLower(map);
                    return result;
                }
            }
        }
        return null;
    }

    private static Map<String,Object> toLower(Map<String, Object> map) {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            if(map != null && !map.isEmpty()){
                Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
                while(iterator.hasNext()) {
                    Map.Entry<String, Object> entry = iterator.next();
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    String firstChar = String.valueOf(key.charAt(0));
                    key = key.replaceFirst(firstChar, firstChar.toLowerCase());
                    if (value != null){
                        result.put(key, String.valueOf(entry.getValue()));
                    }else {
                        result.put(key,null);
                    }

                    if(value instanceof List){
                        List<Map<String,Object>> list = (List<Map<String, Object>>) value;
                        List<Map<String,Object>> listRes = new ArrayList<Map<String,Object>>();
                        if(list != null && list.size() > 0){
                            for (Map<String, Object> map2 : list) {
                                Map<String,Object> mapres = (Map<String, Object>) keyFirstToLower(map2);
                                listRes.add(mapres);
                            }
                            result.put(key, listRes);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}